package org.hefan.org.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.hefan.basic.util.AjaxResult;
import org.hefan.basic.util.PageList;
import org.hefan.org.domain.Employee;
import org.hefan.org.query.EmployeeQuery;
import org.hefan.org.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 接口类
 */
@RestController
@RequestMapping("/employee")
@Api(tags = "员工接口",description = "员工接口详细描述")
public class EmployeeController {

    /**
     * 注入employeeService
     */
    @Autowired
    private IEmployeeService employeeService;

    /**
     * 查询全部
     * //处理请求：Get http://localhost:8080/employee
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询全部员工",notes = "")
    public List<Employee> findAll() {
        return employeeService.loadAll();
    }

    /**
     * 查询单个Employee
     * 处理请求：Get http://localhost:8080/employee/1
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个员工",notes = "需要传入id")
    public Employee findOne(@PathVariable("id")Long id) {
        return employeeService.loadById(id);
    }

    /**
     * 分页查询
     * post请求,参数在请求体中,通过@RequestBody获取请求体中的参数
     * @param employeeQuery 分页查询参数
     * @return
     */
    @PostMapping
    @ApiOperation(value = "通过分页查询员工",notes = "需要传入分页数据")
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery employeeQuery) {
        return employeeService.queryPage(employeeQuery);
    }

    /**
     * 删除员工
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除员工",notes = "通过id删除")
    public AjaxResult delete(@PathVariable("id")Long id) {
        try {
            employeeService.remove(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        }
    }

    /**
     * 添加或修改员工
     * @param employee
     * @return
     */
    @PutMapping
    @ApiOperation(value = "添加或修改员工",notes = "有id则为修改,没有id则是添加")
    public AjaxResult saveOrUpdate(@RequestBody Employee employee) {
        try {
            if (employee.getId() == null) {
                employeeService.save(employee);
            } else {
                employeeService.update(employee);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        }
    }

    /**
     * 批量删除员工
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除员工",notes = "传需要批量删除的员工id")
    public AjaxResult batchRemove(@RequestBody List<Long> ids) {
        try {
            employeeService.batchRemove(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        }
    }
}
